#include <cstdio>
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

vector<vector<unsigned> > m, s;

unsigned bottom_up(vector<unsigned>& p){
   unsigned n = 	p.size()-1;

   m.resize(n+1);
   s.resize(n+1);

   for(unsigned i = 0; i <= n; i++){
      m[i].assign(n+1, 0);
      s[i].assign(n+1, 0);
   }
	
   for(unsigned l = 2; l <= n; l++){
      for(unsigned i = 1; i <= n-l+1; i++){
         unsigned j = i+l-1;
         m[i][j] = numeric_limits<unsigned>::max();

         for(unsigned k = i; k < j; k++){
            unsigned q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];

            if(q < m[i][j]){
               m[i][j] = q;
               s[i][j] = k;
            }
         }
      } 
   }
   
   return m[1][n];
}

unsigned recursive_top_down(vector<unsigned>& p, unsigned i, unsigned j){
   if(m[i][j] < numeric_limits<unsigned>::max()){
      return m[i][j];
   }
   if(i == j){
      m[i][j] = 0;
   } else {
      for(unsigned k = i; k < j; k++){
         unsigned q = recursive_top_down(p, i, k) + recursive_top_down(p, k+1, j) + p[i-1] * p[k] * p[j];
         
         if(q < m[i][j]){
            m[i][j] = q;
            s[i][j] = k;
         }
      }
   }
   
   return m[i][j];
}

unsigned top_down(vector<unsigned>& p){
   unsigned n = p.size()-1;

   m.resize(n+1);
   s.resize(n+1);

   for(unsigned i = 0; i <= n; i++){
      m[i].assign(n+1, numeric_limits<unsigned>::max());
      s[i].assign(n+1, 0);
   }   
   
   return recursive_top_down(p, 1, n);
}

void print(int i, int j){
   if(i == j){
      printf("A%d", i);
   } else {
      printf("(");
      print(i, s[i][j]);
      printf(" x ");
      print(s[i][j]+1, j);		
      printf(")");
   }
}

int main(){
   unsigned n, r, c, count = 0;
   
   cin >> n;  
   while(n != 0){
      vector<unsigned> in;
      
      for(unsigned i = 0; i < n; i++){
         cin >> r >> c;
         in.push_back(r);
      }

      in.push_back(c);
      
      #ifdef TOPDOWN
         top_down(in);
      #else 
         bottom_up(in);
      #endif

      cout << "Case " << ++count << ": ";
      print(1, n);
      cout << endl;
      cin >> n;
   }
   
   return 0;
}

